#include "clientHead/order_analyse.h"



int order_judg(train_chdata* ch, order_train_t* od){
    char* buf = strtok(ch," ");
    
    if(buf[strlen(buf)-1] == '\n'){
        if((strcmp(buf,"ls\n") == 0)||(strcmp(buf,"pwd\n") == 0)){
            buf[strlen(buf)-1] = '\0';
        }
        else{
            printf("该指令需要携带参数!\n");
            return -1;
        }
    }
    
    od->order_len = orderAnalHash(buf);
    buf = strtok(NULL, " ");
    if(buf == NULL){
        od->data_len = 0;
    }
    else{
      od->data_len = strlen(buf);
      strcpy(od->train_data, buf);
    }
    return 0;
}


int hashOpcode(train_chdata* ch){
    int hashValue = 0;
    char *p =ch;
    while (*p != '\0') {
        hashValue += (int) *p++;
    }
    return hashValue % HASH_COUNT;
}
 
TokenType_ orderAnalHash(train_chdata* ch){
    int hash=hashOpcode(ch);
    if(strcmp(order_list[hash].orderCh, ch) == 0) {
        // 哈希判断指令是否存在
        return order_list[hash].orderType;
    }
    return ORDER_ERROR;
}

